package 牛客题霸;


import java.util.Map;
import java.util.TreeMap;

public class 单链表的排序 {

    public ListNode sortInList(ListNode head) {
        if (head == null) {
            return null;
        }
        Map<Integer, Integer> map = new TreeMap<>();
        while (head != null) {
            if (map.containsKey(head.val)) {
                map.put(head.val, map.get(head.val) + 1);
            } else {
                map.put(head.val, 1);
            }
            head = head.next;
        }
        ListNode dum = new ListNode(-1);
        ListNode cur = dum;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                Integer i = entry.getValue();
                while (i > 0) {
                    cur.next = new ListNode(entry.getKey());
                    i--;
                    cur = cur.next;
                }
            } else {
                cur.next = new ListNode(entry.getKey());
                cur = cur.next;
            }
        }
        return dum.next;
    }

    public static void main(String[] args) {

    }

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
